package com.SMP.controller.class_room;

import com.SMP.model.Lessons;
import com.SMP.model.Msg;
import com.SMP.service.class_room.LessonsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
public class LessonsController {
    @Autowired
    LessonsService lessonsService;

    /**
     * @author TSY
    // * @param 分页查询所有课节
     */
    @RequestMapping("/selLessons")
    @ResponseBody

    public Msg findLessons(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 5);
        List<Lessons> lessons = lessonsService.getAllLes();
        PageInfo page = new PageInfo(lessons, 5);
        return Msg.success().add("pageInfo", page);
    }
    /**
     * @author TSY
    // * @param 查询所有课节
     */
    @RequestMapping("/selLes")
    @ResponseBody

    public Msg findLes(){
        List<Lessons> lessons = lessonsService.getAllLes();
        return Msg.success().add("lessons", lessons);
    }
    /**
     * @author TSY
    // * @param 添加课节
     */
    @RequestMapping(value="/addLes")
    @ResponseBody
    public Msg addLessons(Lessons lessons){
        lessons.setLessonsId("3");
        lessons.setLessonsName("第七课节");
        int a= lessonsService.addLessons(lessons);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
    /**
     * @author TSY
    // * @param 按照Id查询课节
     */
    @RequestMapping(value="/lessonWithId")
    @ResponseBody
    public Msg getLessons(@RequestParam(value="lessonsId")String lessonsId){

        Lessons lessons = lessonsService.getLessons(lessonsId);
        return Msg.success().add("lessons", lessons);
    }
    /**
     * @author TSY
    // * @param 按照名字查询课节
     */
    @RequestMapping(value="/lessonByName")
    @ResponseBody
    public Msg selLessonByName(@RequestParam(value="lessonsName")String lessonsName){

        Lessons lessons = lessonsService.selLessonByName(lessonsName);
        return Msg.success().add("lessons", lessons);
    }
    /**
     * @author TSY
    // * @param 修改课节
     */
    @ResponseBody
    @RequestMapping(value="/updLessons")
    public Msg updateLessons(Lessons lessons){
        lessons.setLessonsId("1");
        int a= lessonsService.updateLessons(lessons);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
    /**
     * @author TSY
    // * @param 删除课节(假删)
     */
    @ResponseBody
    @RequestMapping(value="/deleteWithId")
    public Msg deleteLessons(@RequestParam(value="lessonsId")String lessonsId){
        int a=lessonsService.deleteLessons(lessonsId);
        if (a==1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }


}
